package proj2501.simple_module;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JToggleButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class P015Component extends JPanel {
	// TODO: JPopupMenu, JDesktopPane, FileSystemView
	private Page00 page00 = new Page00();
	private Page01 page01 = new Page01();
	public P015Component() {
		System.out.println("Components.");
		setLayout(new BorderLayout());
		JMenuBar00 menuBar = new JMenuBar00();
		add(menuBar, BorderLayout.NORTH);
		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.add("JTabbedPane tab 0", page00);
		tabbedPane.add("tab 1", page01);
		add(tabbedPane);
	}
	
	private class JMenuBar00 extends JMenuBar {
		public JMenuBar00() {
			JMenu menu00 = new JMenu("JMenu");
			JMenuItem menuItem00 = new JMenuItem();
			menuItem00.setAction(new AbstractAction("JMenuItem") {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println("JMenuItem - Action.actionPerformed()");
				}
			});
			menu00.add(menuItem00);
			
			JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem("JCheckBoxMenuItem");
			menu00.add(checkBoxMenuItem);
			
			JRadioButtonMenuItem radioButtonMenuItem00 = new JRadioButtonMenuItem("JRadioButtonMenuItem 0");
			JRadioButtonMenuItem radioButtonMenuItem01 = new JRadioButtonMenuItem("JRadioButtonMenuItem 1");
			menu00.add(radioButtonMenuItem00);
			menu00.add(radioButtonMenuItem01);
			ButtonGroup buttonGroup = new ButtonGroup();
			buttonGroup.add(radioButtonMenuItem00);
			buttonGroup.add(radioButtonMenuItem01);
			
			menu00.addSeparator();
			JMenu menu01 = new JMenu("JMenu");
			JMenuItem menuItem01 = new JMenuItem("JMenuItem");
			menu01.add(menuItem01);
			menu00.add(menu01);
			JMenu menu02 = new JMenu("JMenu");
			JMenuItem menuItem02 = new JMenuItem("JMenuItem");
			menu02.add(menuItem02);
			menu01.add(menu02);
			
			add(menu00);
		}
	}
	
	private class Page00 extends JPanel {
		public Page00() {
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			
			JLabel label = new JLabel("JLabel: hello, world");
			add(label);
			
			JButton button = new JButton("JButton (Alt + B)");
			button.setMnemonic(KeyEvent.VK_B);
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println("Executing ActionListener.actionPerformed method");
				}
			});
			add(button);
			
			JCheckBox checkBox = new JCheckBox("JCheckBox");
			checkBox.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					int stateChange = e.getStateChange();
					if (stateChange == ItemEvent.SELECTED) {
						System.out.println("JCheckBox - ItemListener.itemStateChanged() - ItemEvent.SELECTED");
					} else if (stateChange == ItemEvent.DESELECTED) {
						System.out.println("JCheckBox - ItemListener.itemStateChanged() - ItemEvent.DESELECTED");
					}
				}
			});
			add(checkBox);
			
			JRadioButton radioButton0 = new JRadioButton("JRadioButton 0");
			JRadioButton radioButton1 = new JRadioButton("JRadioButton 1");
			ItemListener radioButtonItemListener = new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					JRadioButton source = (JRadioButton) e.getSource();
					String text = source.getText();
					int stateChange = e.getStateChange();
					if (stateChange == ItemEvent.SELECTED) {
						System.out.println(text + " - ItemListener.itemStateChanged() - ItemEvent.SELECTED");
					} else if (stateChange == ItemEvent.DESELECTED) {
						System.out.println(text + " - ItemListener.itemStateChanged() - ItemEvent.DESELECTED");
					}
				}
			};
			radioButton0.addItemListener(radioButtonItemListener);
			radioButton1.addItemListener(radioButtonItemListener);
			add(radioButton0);
			add(radioButton1);
			ButtonGroup buttonGroup = new ButtonGroup();
			buttonGroup.add(radioButton0);
			buttonGroup.add(radioButton1);
			
			JToggleButton toggleButton = new JToggleButton("JToggleButton");
			toggleButton.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					int stateChange = e.getStateChange();
					if (stateChange == ItemEvent.SELECTED) {
						System.out.println("JToggleButton - ItemListener.itemStateChanged() - ItemEvent.SELECTED");
					} else if (stateChange == ItemEvent.DESELECTED) {
						System.out.println("JToggleButton - ItemListener.itemStateChanged() - ItemEvent.DESELECTED");
					}
				}
			});
			toggleButton.setToolTipText("The JToggleButton class is the superclass of the JCheckBox and JRadioButton classes.");
			add(toggleButton);
			
			JComboBox<Object> comboBox = new JComboBox<>();
			comboBox.setAlignmentX(Component.LEFT_ALIGNMENT);
			comboBox.addItem("Item 0");
			comboBox.addItem("Item 1");
			comboBox.addItem("Item 2");
			comboBox.setMaximumSize(comboBox.getPreferredSize());
			comboBox.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					if (e.getStateChange() == ItemEvent.SELECTED) {
						System.out.println("JComboBox - " + e.getItem() + " is selected.");
					}
				}
			});
			add(comboBox);
			
			JSlider slider = new JSlider();
			slider.setAlignmentX(Component.LEFT_ALIGNMENT);
			slider.setValue(0);
			slider.addChangeListener(new ChangeListener() {
				@Override
				public void stateChanged(ChangeEvent e) {
					JSlider slider = (JSlider) e.getSource();
					int value = slider.getValue();
					System.out.println("JSlider.getValue(): " + value);
				}
			});
			slider.setToolTipText("JSlider");
			add(slider);
			
			double dummyMaxForPreferredSize = 12345;
			SpinnerNumberModel spinnerNumberModel = new SpinnerNumberModel(0, -11, dummyMaxForPreferredSize, 1.1);
			JSpinner spinner = new JSpinner(spinnerNumberModel);
			spinnerNumberModel.setMaximum(new Double(11));
			spinner.setMaximumSize(spinner.getPreferredSize());
			spinner.setAlignmentX(Component.LEFT_ALIGNMENT);
			spinner.addChangeListener(new ChangeListener() {
				@Override
				public void stateChanged(ChangeEvent e) {
					JSpinner spinner = (JSpinner) e.getSource();
					double value = (double) spinner.getValue();
					System.out.println("JSpinner.getValue(): " + value);
				}
			});
			spinner.setToolTipText("JSpinner");
			add(spinner);
			
			final JProgressBar progressBar = new JProgressBar(0, 1000);
			progressBar.setAlignmentX(Component.LEFT_ALIGNMENT);
			progressBar.setStringPainted(true);
			Timer timer = new Timer(10, new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					int value = progressBar.getValue();
					value++;
					value = value % progressBar.getMaximum();
					progressBar.setValue(value);
					progressBar.repaint();
				}
			});
			timer.start();
			progressBar.setToolTipText("JProgressBar");
			add(progressBar);
			
			JProgressBar indeterminate = new JProgressBar();
			indeterminate.setAlignmentX(Component.LEFT_ALIGNMENT);
			indeterminate.setIndeterminate(true);
			indeterminate.setToolTipText("JProgressBar.setIndeterminate(true)");
			add(indeterminate);
		}
	}
	private class Page01 extends JPanel {
		public Page01() {
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			
			JButton button = new JButton("JOptionPane.showMessageDialog()");
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(Page01.this, "message");
				}
			});
			add(button);
			
			button = new JButton("JOptionPane.showInputDialog() - JComboBox");
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					Object[] objects = {"JOptionPane.ERROR_MESSAGE", "JOptionPane.INFORMATION_MESSAGE",
							"JOptionPane.QUESTION_MESSAGE", "JOptionPane.WARNING_MESSAGE", "JOptionPane.PLAIN_MESSAGE"};
					Object result = JOptionPane.showInputDialog(Page01.this, "message", "title",
							JOptionPane.PLAIN_MESSAGE, null, objects, null);
					String string = (String) result;
					if (string.equals("JOptionPane.ERROR_MESSAGE")) {
						JOptionPane.showMessageDialog(Page01.this, "message", "title", JOptionPane.ERROR_MESSAGE);
					} else if (string.equals("JOptionPane.INFORMATION_MESSAGE")) {
						JOptionPane.showMessageDialog(Page01.this, "message", "title", JOptionPane.INFORMATION_MESSAGE);
					} else if (string.equals("JOptionPane.QUESTION_MESSAGE")) {
						JOptionPane.showMessageDialog(Page01.this, "message", "title", JOptionPane.QUESTION_MESSAGE);
					} else if (string.equals("JOptionPane.WARNING_MESSAGE")) {
						JOptionPane.showMessageDialog(Page01.this, "message", "title", JOptionPane.WARNING_MESSAGE);
					} else if (string.equals("JOptionPane.PLAIN_MESSAGE")) {
						JOptionPane.showMessageDialog(Page01.this, "message", "title", JOptionPane.PLAIN_MESSAGE);
					}
				}
			});
			add(button);
			
			button = new JButton("JOptionPane.showInputDialog()");
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String string = JOptionPane.showInputDialog(Page01.this, "message");
					JOptionPane.showMessageDialog(Page01.this, "Input: " + string);
				}
			});
			add(button);
			
			button = new JButton("JOptionPane.showConfirmDialog()");
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					int result = JOptionPane.showConfirmDialog(Page01.this, "Confirm?");
//					JOptionPane.showMessageDialog(Page01.this, "Input: " + string);
					if (result == JOptionPane.CLOSED_OPTION) {
						JOptionPane.showMessageDialog(Page01.this, "JOptionPane.CLOSED_OPTION");
					} else if (result == JOptionPane.CANCEL_OPTION) {
						JOptionPane.showMessageDialog(Page01.this, "JOptionPane.CANCEL_OPTION");
					} else if (result == JOptionPane.NO_OPTION) {
						JOptionPane.showMessageDialog(Page01.this, "JOptionPane.NO_OPTION");
					} else if (result == JOptionPane.OK_OPTION) {
						JOptionPane.showMessageDialog(Page01.this, "JOptionPane.OK_OPTION");
					}
				}
			});
			add(button);
			
			button = new JButton("JColorChooser");
			button.setMnemonic(KeyEvent.VK_C);
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					Color color = JColorChooser.showDialog(Page01.this, null, null);
					System.out.println("JColorChooser returned: " + color);
				}
			});
			add(button);
			
			JPanel cursorPanel = new JPanel();
			cursorPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
			cursorPanel.setLayout(new BoxLayout(cursorPanel, BoxLayout.X_AXIS));
			JLabel cursorLabel = new JLabel("Cursor: ");
			cursorPanel.add(cursorLabel);
			JComboBox<Object> comboBox = new JComboBox<>();
			comboBox.setAlignmentX(Component.LEFT_ALIGNMENT);
			comboBox.addItem("System Default");
			comboBox.addItem("Cursor.CROSSHAIR_CURSOR");
			comboBox.addItem("Cursor.E_RESIZE_CURSOR");
			comboBox.addItem("Cursor.HAND_CURSOR");
			comboBox.addItem("Cursor.MOVE_CURSOR");
			comboBox.addItem("Cursor.N_RESIZE_CURSOR");
			comboBox.addItem("Cursor.NE_RESIZE_CURSOR");
			comboBox.addItem("Cursor.NW_RESIZE_CURSOR");
			comboBox.addItem("Cursor.S_RESIZE_CURSOR");
			comboBox.addItem("Cursor.SE_RESIZE_CURSOR");
			comboBox.addItem("Cursor.SW_RESIZE_CURSOR");
			comboBox.addItem("Cursor.TEXT_CURSOR");
			comboBox.addItem("Cursor.W_RESIZE_CURSOR");
			comboBox.addItem("Cursor.WAIT_CURSOR");
			comboBox.addItem("Hide Cursor (5 seconds)");
			comboBox.setMaximumSize(comboBox.getPreferredSize());
			comboBox.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					if (e.getStateChange() == ItemEvent.SELECTED) {
//						System.out.println("JComboBox - " + e.getItem() + " is selected.");
						String string = (String) e.getItem();
						if (string.equals("System Default")) {
							Cursor cursor = Cursor.getDefaultCursor();
							setCursor(cursor);
						} else if (string.equals("Cursor.CROSSHAIR_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.E_RESIZE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.HAND_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.MOVE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.N_RESIZE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.NE_RESIZE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.NW_RESIZE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.S_RESIZE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.SE_RESIZE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.SW_RESIZE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.TEXT_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.W_RESIZE_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Cursor.WAIT_CURSOR")) {
							Cursor cursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
							setCursor(cursor);
						} else if (string.equals("Hide Cursor (5 seconds)")) {
							BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
							Toolkit toolkit = Toolkit.getDefaultToolkit();
							Cursor cursor = toolkit.createCustomCursor(image, new Point(0, 0), null);
							System.out.println("Hide Cursor");
							setCursor(cursor);
							Timer timer = new Timer(5000, new ActionListener() {
								@Override
								public void actionPerformed(ActionEvent e) {
									Cursor cursor = Cursor.getDefaultCursor();
									Page01.this.setCursor(cursor);
									System.out.println("Show Cursor");
								}
							});
							timer.setRepeats(false);
							timer.start();
						}
					}
				}
			});
			cursorPanel.add(comboBox);
			add(cursorPanel);

		}
	}
}
